import {createStore, applyMiddleware} from "redux";
import {useRouterHistory} from "react-router";
import {routerMiddleware} from 'react-router-redux'
// import createHashHistory from 'history/lib/createHashHistory'
import createBrowserHistory from 'history/lib/createBrowserHistory'
import thunkMiddleware from "redux-thunk";
import rootReducer from '@reducers'

const nextReducer = require('@reducers')

const logger = () => (store) => (next) => (action) => {
    return next(action)
}

function configureStore(initialState) {
    const create = window.devToolsExtension
        ? window.devToolsExtension()(createStore)
        : createStore
    // const history = useRouterHistory(createHashHistory)({})
    const history = useRouterHistory(createBrowserHistory)({})
    const reduxRouterMiddleware = routerMiddleware(history)
    const createStoreWithMiddleware = applyMiddleware(
        reduxRouterMiddleware,
        thunkMiddleware,
        logger()
    )(create)
    const store = createStoreWithMiddleware(rootReducer, initialState)
    if (module.hot) {
        module.hot.accept('../reducers', () => {
            store.replaceReducer(nextReducer)
        })
    }
    return store
}

export default configureStore
